import BESA.ExceptionBESA;
import BESA.Kernell.Agent.Event.EventBESA;
import BESA.Kernell.Agent.PeriodicGuardBESA;
import BESA.Kernell.Agent.StructBESA;
import BESA.Kernell.System.AdmBESA;
import BESA.Kernell.System.Directory.AgHandlerBESA;
import BESA.Util.PeriodicDataBESA;
import Vehiculo.SensorGuard;
import Vehiculo.AgenteVehiculo;

import Vehiculo.EstadoVehiculo;
import World.Behavior.GameGuard;
import World.Behavior.SubscribeGuard;
import World.Behavior.UpdateGuard;
import World.State.WorldState;
import World.WorldAgent;
import mundo.MundoGUI;
import mundo.Punto;

import Semaforo.EstadoSemaforo;
import Semaforo.AgenteSemaforo;
import mundo.PanelMundo;
import mundo.*;

import Semaforo.*;
import java.util.List;
import java.util.Random;
/**
 *
 * @author Andres
 */
public class EjemploBESA {

    public static int GAME_PERIODIC_TIME = 1000;
    public static int GAME_PERIODIC_DELAY_TIME = 100;
    
    public static void main(String[] args) throws ExceptionBESA {
        MundoGUI frame = new MundoGUI();
        frame.setVisible(true);
        AdmBESA admLocal = AdmBESA.getInstance();
        WorldState ws = new WorldState(11, 11, frame.getPanelMundo());
        StructBESA wrlStruct = new StructBESA();
        wrlStruct.addBehavior("WorldBehavior");
        wrlStruct.bindGuard("WorldBehavior", GameGuard.class);
        wrlStruct.addBehavior("ChangeBehavior");
        wrlStruct.bindGuard("ChangeBehavior", SubscribeGuard.class);
        wrlStruct.bindGuard("ChangeBehavior", UpdateGuard.class);      
        WorldAgent wa = new WorldAgent("WORLD", ws, wrlStruct, 0.91); 
        wa.start();
        
        
        EstadoSemaforo s1State = new EstadoSemaforo();
        StructBESA s1Struct = new StructBESA();
        s1Struct.addBehavior("playerPerception2");
        s1Struct.bindGuard("playerPerception2", SensorGuardSemaforo.class);
        AgenteSemaforo As1 = new AgenteSemaforo("semaforo_0", s1State, s1Struct, 0.91);
        As1.start();
        
        EstadoSemaforo s2State = new EstadoSemaforo();
        StructBESA s2Struct = new StructBESA();
        s2Struct.addBehavior("playerPerception2");
        s2Struct.bindGuard("playerPerception2", SensorGuardSemaforo.class);
        AgenteSemaforo As2 = new AgenteSemaforo("semaforo_1", s2State, s2Struct, 0.91);
        As2.start();
        
        Random randomno = new Random();
        
        

       EstadoVehiculo ctState;
       StructBESA ctStruct;
       AgenteVehiculo vhst; 
       int r=0;
      
       List<Tramo> c=frame.getPanelMundo().getMalla().getListaTramos();
        for(int i=0;i<c.size();i++){
            Punto p1 = null;
            r = i % 2;
            if( r == 0 ){
                p1 = c.get(i).getInfIz();
            }else{
                p1 = c.get(i).getSupDer();
            }
          
        ctState= new EstadoVehiculo(p1, new Punto(randomno.nextInt(20), randomno.nextInt(20)));
            ctStruct= new StructBESA();
            ctStruct.addBehavior("playerPerception");
            ctStruct.bindGuard("playerPerception", SensorGuard.class);
            vhst= new AgenteVehiculo("carro_"+(i+1), ctState, ctStruct, 0.91);
            vhst.start();
        }
        
        /*
        EstadoVehiculo c2State = new EstadoVehiculo(new Punto(2, 3), new Punto(4, 4));
        StructBESA c2Struct = new StructBESA();
        c2Struct.addBehavior("playerPerception");
        c2Struct.bindGuard("playerPerception", SensorGuard.class);
        AgenteVehiculo vh2 = new AgenteVehiculo("vh2", c2State, c2Struct, 0.91);
        vh2.start();
        */
        
        EstadoVehiculo c1State = new EstadoVehiculo(new Punto(1, 3), new Punto(18,8));
        StructBESA c1Struct = new StructBESA();
        c1Struct.addBehavior("playerPerception");
        c1Struct.bindGuard("playerPerception", SensorGuard.class);
        AgenteVehiculo vh1 = new AgenteVehiculo("ambulancia_0", c1State, c1Struct, 0.91);
        vh1.start();
        
        /*
        EstadoVehiculo c3State = new EstadoVehiculo(new Punto(3, 7), new Punto(4, 4));
        StructBESA c3Struct = new StructBE4SA();
        c3Struct.addBehavior("playerPerception");
        c3Struct.bindGuard("playerPerception", SensorGuard.class);
        AgenteVehiculo vh3 = new AgenteVehiculo("vh3", c3State, c3Struct, 0.91);
        vh3.start();
        
        EstadoVehiculo c4State = new EstadoVehiculo(new Punto(13, 7), new Punto(4, 4));
        StructBESA c4Struct = new StructBESA();
        c4Struct.addBehavior("playerPerception");
        c4Struct.bindGuard("playerPerception", SensorGuard.class);
        AgenteVehiculo vh4 = new AgenteVehiculo("vh4", c4State, c4Struct, 0.91);
        vh4.start();
        
        EstadoVehiculo c5State = new EstadoVehiculo(new Punto(5, 7), new Punto(4, 4));
        StructBESA c5Struct = new StructBESA();
        c5Struct.addBehavior("playerPerception");
        c5Struct.bindGuard("playerPerception", SensorGuard.class);
        AgenteVehiculo vh5 = new AgenteVehiculo("vh5", c5State, c5Struct, 0.91);
        vh5.start();
        
        EstadoVehiculo c6State = new EstadoVehiculo(new Punto(7, 7), new Punto(4, 4));
        StructBESA c6Struct = new StructBESA();
        c6Struct.addBehavior("playerPerception");
        c6Struct.bindGuard("playerPerception", SensorGuard.class);
        AgenteVehiculo vh6 = new AgenteVehiculo("vh6", c6State, c6Struct, 0.91);
        vh6.start();
        
        EstadoVehiculo c7State = new EstadoVehiculo(new Punto(15, 7), new Punto(4, 4));
        StructBESA c7Struct = new StructBESA();
        c7Struct.addBehavior("playerPerception");
        c7Struct.bindGuard("playerPerception", SensorGuard.class);
        AgenteVehiculo vh7 = new AgenteVehiculo("vh7", c7State, c7Struct, 0.91);
        vh7.start();
        */
        PeriodicDataBESA data  = new PeriodicDataBESA(GAME_PERIODIC_TIME, GAME_PERIODIC_DELAY_TIME, PeriodicGuardBESA.START_PERIODIC_CALL);
        EventBESA startPeriodicEv = new EventBESA(GameGuard.class.getName(), data);
        AgHandlerBESA ah = admLocal.getHandlerByAlias("WORLD");
        ah.sendEvent(startPeriodicEv);

        
    }

}
